<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="URL" prop="slug">
        <el-input
          v-model="queryParams.slug"
          placeholder="友好URL"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="authorName">
        <el-input
          v-model="queryParams.authorName"
          placeholder="请输入作者名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in ver_blog_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="阅读量" prop="viewCount">
        <el-input
          v-model="queryParams.viewCount"
          placeholder="请输入阅读量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" style="width: 308px">
        <el-date-picker
          v-model="daterangePublishTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:verBlog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:verBlog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:verBlog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:verBlog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表表格 -->
    <el-table v-loading="loading" :data="verBlogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="博客唯一ID" align="center" prop="blogId" />
      <el-table-column label="博客标题" align="center" prop="title" />
      <el-table-column label="URL友好标识" align="center" prop="slug" />
      <el-table-column label="博客封面图" align="center" prop="coverImageUrl" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.coverImageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="作者名称" align="center" prop="authorName" />
      <el-table-column label="发布状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="ver_blog_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="阅读量" align="center" prop="viewCount" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:verBlog:list']">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:verBlog:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:verBlog:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="VerBlogList">
import { listVerBlog, delVerBlog } from "@/api/system/verBlog";
import { useRouter } from 'vue-router';
import { onMounted, getCurrentInstance } from "vue";

const router = useRouter();
const { proxy } = getCurrentInstance();
const { ver_blog_status } = proxy.useDict('ver_blog_status');

// 列表相关状态
const verBlogList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

// 日期范围状态
const daterangePublishTime = ref([]);

// 查询参数
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    slug: null,
    authorName: null,
    status: null,
    viewCount: null,
  }
});
const { queryParams } = toRefs(data);

/** 查询博客列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  
  // 处理日期范围参数
  if (daterangePublishTime.value?.length) {
    queryParams.value.params["beginPublishTime"] = daterangePublishTime.value[0];
    queryParams.value.params["endPublishTime"] = daterangePublishTime.value[1];
  }

  listVerBlog(queryParams.value).then(response => {
    verBlogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangePublishTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.blogId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 - 跳转到新增页面 */
function handleAdd() {
  router.push({ path: '/system/ver-blog/add' });
}

/** 修改按钮操作 - 跳转到编辑页面 */
function handleUpdate(row) {
  const blogId = row ? row.blogId : ids.value[0];
  router.push({ path: `/system/ver-blog/edit/${blogId}` });
}

/** 详情按钮操作 - 跳转到详情页面 */
function handleDetail(row) {
  const blogId = row ? row.blogId : ids.value[0];
  router.push({ path: `/system/ver-blog/detail/${blogId}` });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _blogIds = row ? [row.blogId] : ids.value;
  proxy.$modal.confirm(`是否确认删除博客编号为"${_blogIds}"的数据项？`).then(() => {
    return delVerBlog(_blogIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/verBlog/export', {
    ...queryParams.value
  }, `verBlog_${new Date().getTime()}.xlsx`);
}

// 初始化加载列表
onMounted(() => {
  getList();
});
</script>
